package cn.edu.gzhu.workOrderManagement.intercepter;

import cn.edu.gzhu.workOrderManagement.constants.MessageConstant;
import cn.edu.gzhu.workOrderManagement.enumeration.UserAuthorityEnum;
import cn.edu.gzhu.workOrderManagement.util.ResponseUtil;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
@Slf4j
//只有总部商务和客户可以创建工单
public class WorkOrderCreateInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
        Claims claims =(Claims)request.getAttribute("claims");
        if (claims==null){
            return false;
        }
        UserAuthorityEnum authority = UserAuthorityEnum.value2Object(claims.get("authority").toString());
        if (!UserAuthorityEnum.HEAD_BUSINESS.equals(authority)&&!UserAuthorityEnum.CLIENT.equals(authority)){
            ResponseUtil.failResponse(response, MessageConstant.PERMISSION_DENIED);
            return false;
        }

        return true;
    }
}
